<%@ page contentType="text/html;charset=UTF-8" language="java" pageEncoding="UTF-8" %>
<style type="text/css">
    .datagrid-tree-level-1 .datagrid-tree-td{
        padding-left: 25px !important;
    }
    .datagrid-tree-level-2 .datagrid-tree-td{
        padding-left: 25px !important;
    }
    .datagrid-tree-level-3 .datagrid-tree-td{
        padding-left: 25px !important;
    }
</style>
<script type="text/javascript">
    // department
    function datagrid_tree_isButton() {
        return [{'1': '是'}, {'0': '否'}]
    }
    // 操作列
    function menuOperation(value, data) {
        var html = '<button type="button" class="btn-green" onclick="saveMenu(\'\',\'' + data.id + '\');">添加</button>' +
            '<button type="button" class="btn-green" onclick="saveMenu(\'' + data.id + '\',\'\');">编辑</button>'
            + '<button type="button" class="btn-red" data-toggle="del.datagrid.tr">删除</button>';
        return html
    }
    //不能拖动一级父节点
    function datagrid_tree_beforeDrag(tr, data) {
        if (data && data.level == 0) {
            return false
        }
        return true
    }
    // 不能将子节点拖为一级父节点
    function datagrid_tree_beforeDrop(data, targetData, position) {
        if (targetData && targetData.level == 0 && position !== 'append') {
            return false
        }
        return true
    }
</script>
<div class="bjui-pageContent">
    <table id="menu_table_cl" class="table table-bordered">

    </table>
</div>
<script>

    $('#menu_table_cl').datagrid({
        height: '100%',
        tableWidth: '100%',
        gridTitle: '菜单 ',
        showToolbar: true,
        filterThead:false,
        toolbarItem: 'refresh,|,del',
        toolbarCustom:function(){
            return '<button type="button" class="btn btn-blue" onclick="saveMenu(\'\',\'0\');" data-icon="plus"><i class="fa fa-plus"></i> 添加</button>';
        },
        local: 'local',
        sortAll: true,
        dataUrl: '/admin/menu/menus',
        inlineEditMult: false,
        editUrl: '/admin/menu/save',
        delUrl: '/admin/menu/delete',
        delConfirm: true,
        delCallback: function (response) {
            if (response.haveError) {
                BJUI.alertmsg('error', response.errMsg, {
                    okCall: function () {
                        $('#menu_table_cl').datagrid("refresh",true);
                    }
                });
            } else {
                BJUI.alertmsg('ok', response.data, {
                    okCall: function () {
                        $('#menu_table_cl').datagrid("refresh",true);
                    }
                });
            }
        },
        delPK: 'id',
        isTree: 'name',
        addLocation: 'last',
        fieldSortable: false,
        columnMenu: false,
        paging:false,
        treeOptions: {
            expandAll: false,
            add: false,
            simpleData: true,
            keys: {
                parentKey: 'parentId'
            },
        },
        /*dropOptions: {
            drop: true,
            position: 'before',
            dropUrl: '/json/ajaxDone.json',
            beforeDrag: datagrid_tree_beforeDrag,
            beforeDrop: datagrid_tree_beforeDrop,
            afterDrop: 'array'
        },*/
        columns: [
            {name: 'name', align: 'center', label: '菜单名称'},
            {name: 'nid', align: 'center', label: '权限标识'},
            /*{
                name: 'parentId',
                label: '父级id',
                align: 'center',
                rule: 'required',
                hide:true,
                attrs: {readonly: 'readonly'}
            },*/
            {name: 'sort', label: '排序', align: 'center', rule: 'required'},
            {name: 'url', label: '链接地址', align: 'center', rule: 'required'},
            {render: menuOperation, label: '操作列'}
        ]
    });


    function saveMenu(id,parentId) {
        var savePageUrl = "/admin/menu/savePage";
        if(id!=""&&id!=null&&typeof (id) != "undefined" ){
            savePageUrl = savePageUrl + "?id="+id;
        }
        if(parentId!=""&&parentId!=null&&typeof (parentId) != "undefined" ){
            savePageUrl = savePageUrl + "?parentId="+parentId;
        }
        BJUI.dialog({
            id: 'add_menu',
            url: savePageUrl,
            title: '保存',
            resizable: true,
            width: 600,
            height: 400,
            fresh:true
        });
    };

</script>